//
//  UILabels.swift
//  JXBaseTools
//
//  Created by godox on 2021/11/30.
//

import UIKit


extension JX where Base: UILabel {
    
    /// 设置Label的行间距和字间距
    ///
    /// - Parameters:
    ///   - lineSpace: 行间距
    ///   - wordSpace: 字间距
    func changeLabelRowSpace(lineSpace: CGFloat, wordSpace: CGFloat) {
        guard let content = base.text else {return}
        let attributedString : NSMutableAttributedString = NSMutableAttributedString(string: content)
        let paragraphStyle : NSMutableParagraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpace
        attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, (content.count)))
        attributedString.addAttribute(NSAttributedString.Key.kern, value: wordSpace, range: NSMakeRange(0, (content.count)))
        base.attributedText = attributedString
        base.sizeToFit()
    }
    
    
    /// 指定Label显示的字的个数
    ///
    /// - Parameter number: 个数
    func specifiesTheNumberOfWordsToDisplay(number: Int) {
        guard let content = base.text else {return}
        let attributedString : NSMutableAttributedString = NSMutableAttributedString(string: content)
        if content.count < number {return}
        attributedString.deleteCharacters(in: NSMakeRange(number, content.count - number))
        base.attributedText = attributedString
        base.sizeToFit()
    }
    
}
